package com.aerors.DataProcess;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.gdal.gdal.Dataset;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconstConstants;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

/**
 * 1.矢量和栅格数据的坐标系检查
 * 
 * @author wx
 *
 */
public class CRSCheck {
	private static final String rootPath = "C:\\data\\shapes\\矢量";
	private static final String errorFilePath = "C:\\data\\shapes\\error.txt";
	private static final List<String> errorList = new ArrayList<>();

	static {
		gdal.AllRegister();
	}

	public static void main(String[] args) throws Exception {
		CRSCheck check = new CRSCheck();
		File rootDir = new File(rootPath);
		check.check(rootDir);
	}

	public void check(File rootDir) throws Exception {
		gdal.AllRegister();
		for (File f : rootDir.listFiles()) {
			if (f.isDirectory()) {
				check(f);
			} else {
				boolean checkResult = true;
				String suffix = this.getSuffix(f);
				switch (suffix) {
				case "shp":
					checkResult = this.checkShp(f);
					break;
				case "tif":
					checkResult = this.checkImage(f);
					break;
				case "tiff":
					checkResult = this.checkImage(f);
					break;
				case "img":
					checkResult = this.checkImage(f);
					break;
				case "":
					break;
				default:
					break;
				}
				System.out.println("当前处理:" + f.getAbsolutePath());
				if (!checkResult) {
					errorList.add(f.getAbsolutePath() + ":坐标系错误");
				}
			}
		}

		FileWriter fw = new FileWriter(errorFilePath);
		for(String errorMsg : errorList) {
			fw.write(errorMsg + "\r\n");
		}
		fw.flush();
		fw.close();
		System.out.println("坐标系检查完毕！");
	}

	public String getSuffix(File f) {
		String fName = f.getName();
		if(fName.indexOf(".") > -1) {
			return fName.substring(fName.lastIndexOf(".")+1, fName.length());
		}else {
			return "";
		}
	}

	public boolean checkShp(File f) throws Exception {
		Map<String, Object> map = new HashMap<>();
		map.put("url", f.toURI().toURL());
		DataStore dataStore = DataStoreFinder.getDataStore(map);
		String typeName = dataStore.getTypeNames()[0];
		FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(typeName);
		SimpleFeatureType schema = source.getSchema();
		CoordinateReferenceSystem crs = schema.getCoordinateReferenceSystem();
		String crsStr = crs.toString();
		boolean result = this.crsMather(crsStr);
		return result;
	}

	public boolean checkImage(File f) {
		Dataset dataset = gdal.Open(f.getAbsolutePath(), gdalconstConstants.GA_ReadOnly);
		if (dataset == null) {
			System.out.println("GDALOpen failed - " + gdal.GetLastErrorNo());
			System.out.println(gdal.GetLastErrorMsg());
			errorList.add(f.getAbsolutePath() + ":" + "数据损坏,无法打开");
			return false;
		}
		String crsStr = dataset.GetProjectionRef();
		boolean result = this.crsMather(crsStr);
		return result;
	}

	public boolean crsMather(String crsStr) {
		boolean result = false;
		if (crsStr.startsWith("GEOGCS[\"China Geodetic Coordinate System 2000\",")) {
			result = true;
		}
		return result;
	}
}
